/**
 * ChartData.java
 */
package com.pluginsacceptor.plugin.formula.MathsTrigo.support;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

/**
 * The class contains current chart data and ui state
 */
public class MathData {

    /** Logger */
    private static final Logger LOG = Logger.getLogger(MathData.class);

    /**  */
//    private final List<Double> data = new ArrayList<Double>();
    private final List<String> data = new ArrayList<String>();

    /** The column number for data array */
    private final List<Integer> col = new ArrayList<Integer>();

    /** The rows number for data array */
    private final List<Integer> row = new ArrayList<Integer>();


    /** Clearing */
    public void clear() {
        data.clear();
        row.clear();
        col.clear();
    }
    /**
     * Add chart item
     * @param dataItem the data value
     * @param rowItem the row value
     * @param col the column number
     */
    public void addItem(final String dataItem, final Integer rowItem, final Integer colItem) {
        if (!(this.row.contains(rowItem) && this.col.contains(colItem))) {
            this.data.add(dataItem);
            this.row.add(rowItem);
            this.col.add(colItem);
        }
    }


    /**
     * @return the data
     */
    public List<String> getData() {
        return data;
    }


    /**
     * @return the col
     */
    public List<Integer> getCol() {
        return col;
    }

    /**
     * @return the row
     */
    public List<Integer> getRow() {
        return row;
    }

    /**
     * Silent parse string to double
     * @param data the source string
     * @return number or NAN if string isn't number
     */
    public static double silentParse(final String data) {
        double result;

        try {
            result = Double.parseDouble(data);
        } catch (final Exception e) {
            result = Double.NaN;
            LOG.warn("Invalid data '" + data + "'");
        }

        return result;
    }


    /**
     * Silent parse string to double
     * @param data the source string
     * @return number array
     */
    public static double[] silentParse(final String[] data) {
        final List<Double> number = new ArrayList<Double>();
        for (final String tmp : data) {
            try {
                number.add(Double.parseDouble(tmp));
            } catch (final Exception e) {
                LOG.warn("invalid data value '" + tmp + "'");
            }
        }
        final double[] primNumber = new double[number.size()];
        for (int cnt = primNumber.length; --cnt >= 0;) {
            primNumber[cnt] = number.get(cnt);
        }

        return primNumber;
    }
}

